﻿using CL.Enum.Common;
using CL.Game.BLL;
using CL.Game.BLL.IM;
using CL.Game.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CL.Admin.admin.room
{
    public partial class addroom : UI.AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = Request.QueryString["action"];
            //如果是编辑则检查信息是否存在
            if (_action != CaileEnums.ActionEnum.Add.ToString())
            {
                JscriptMsg(string.Format("权限不足，请联系管理员"), "back");
                return;
            }
            if (!IsPostBack)
            {
                this.BindLottery();
            }
        }
        protected void BindLottery()
        {
            ddlLottery.DataTextField = "LotteryName";
            ddlLottery.DataValueField = "LotteryCode";
            ddlLottery.DataSource = new LotteriesBLL().QueryLotterys();
            ddlLottery.DataBind();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                long Frequency = 0;
                int RoomNums = 0;
                long.TryParse(txtFrequency.Text, out Frequency);
                int.TryParse(txtRoomNums.Text, out RoomNums);
                LotteryRoomEntity Entity = new LotteryRoomEntity();
                Entity.ChatSet = ddlChatSet.SelectedValue;
                Entity.CreateTime = DateTime.Now;
                Entity.CreateUser = 0;
                Entity.Frequency = Frequency;
                Entity.IsEnable = true;
                Entity.LotteryCode = Convert.ToInt32(ddlLottery.SelectedValue);
                Entity.ModifyTime = DateTime.Now;
                Entity.RoomAuthor = 0;
                Entity.RoomName = txtRoomName.Text.Trim();
                Entity.RoomNotice = string.Empty;
                Entity.RoomNums = RoomNums;
                Entity.RoomCode = "0";
                var Rec = new LotteryRoomBLL().InsertEntity(Entity);
                if (Rec > 0)
                {
                    Entity.RoomCode = Rec.ToString();
                    new LotteryRoomBLL().ModifyEntity(Entity);
                    this.SetRebate(Rec);
                    var IMRec = new Communication().SetRoom(Rec.ToString(), Entity.RoomName, "-1", 1, Entity.LotteryCode, 1);
                    if (IMRec)
                    {
                        JscriptMsg(string.Format("新增成功"), "lotteryroomlist.aspx");
                    }
                    else
                        JscriptMsg(string.Format("新增房间失败,IM房间新增失败"), "back");
                }
                else
                    JscriptMsg(string.Format("新增房间失败"), "back");

            }
            catch (Exception ex)
            {
                JscriptMsg(string.Format("新增房间错误：{0}", ex.Message), "back");
            }
        }
        protected void SetRebate(long RoomID)
        {
            var Str = HidRebate.Value;
            if (!string.IsNullOrEmpty(Str))
            {
                List<RoomRebateEntity> Entitys = new List<RoomRebateEntity>();
                RoomRebateEntity Entity = null;
                string[] Rebates = Str.TrimEnd('|').Split('|');
                foreach (string Rebate in Rebates)
                {
                    string[] item = Rebate.Split('^');
                    Entity = new RoomRebateEntity();
                    Entity.RoomID = RoomID;
                    Entity.RoomCode = RoomID.ToString();
                    Entity.MinAmount = Convert.ToInt64(item[0]) * 100;
                    Entity.MaxAmount = Convert.ToInt64(item[1]) * 100;
                    Entity.Rebates = Convert.ToInt64(item[2]);
                    Entity.Remark = item[2];
                    Entity.SetTime = DateTime.Now;
                    Entitys.Add(Entity);
                }
                if (Entitys.Count > 0)
                    new RoomRebateBLL().InsertEntitys(Entitys);
            }
        }
    }
}